﻿using Microsoft.AspNetCore.Authentication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Jackletter.BlendAuth
{
    public class BlendAuthenticationOptions : AuthenticationSchemeOptions
    {
        /// <summary>
        /// 默认登录后滑动窗口时长,默认1天,即: 登录后,如果连续1天不访问就过期
        /// </summary>
        public TimeSpan SlidingTimeSpan { get; set; } = TimeSpan.FromDays(1);

        /// <summary>
        /// 滑动窗口的最小滑动间隔,默认1小时,即: 登录后,如果超过1小时又来访问,那么将自动延期token一个滑动窗口时长
        /// </summary>
        public TimeSpan SlidingTriggerInterval { set; get; } = TimeSpan.FromHours(1);

        /// <summary>
        /// 检查token过期的最小时间间隔
        /// </summary>
        public TimeSpan CheckExpireInterval { set; get; } = TimeSpan.FromHours(1);

        #region token存储位置
        /// <summary>
        /// 是否启用Cookie作为token的载体: 默认为true
        /// </summary>
        public bool EnableTokenInCookie { set; get; } = true;
        /// <summary>
        /// 从Cookie里面取认证信息的名称,默认: aspnetcore.blendauth
        /// </summary>
        public string TokenInCookieName { get; set; } = "aspnetcore.blendauth";

        /// <summary>
        /// 是否启用Http请求头作为token的载体: 默认为true
        /// </summary>
        public bool EnableTokenInHeader { set; get; } = true;
        /// <summary>
        /// 从请求头里面取认证信息的名称,默认: Authorization
        /// </summary>
        public string TokenInHeaderName { get; set; } = "Authorization";

        /// <summary>
        /// 是否启用Url中参数作为token的载体
        /// </summary>
        public bool EnableTokenInUrl { set; get; } = true;
        /// <summary>
        /// 从url取认证信息的参数名称,默认: _blendauth
        /// </summary>
        public string TokenInUrlName { get; set; } = "_blendauth";
        #endregion

        #region token持久化配置
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        internal string DBConn { set; get; }

        /// <summary>
        /// 数据库类型
        /// </summary>
        internal string DBType { set; get; }

        /// <summary>
        /// 使用sqlserver数据库作为token的持久化存储
        /// </summary>
        /// <param name="dbconn">数据库连接字符串</param>
        /// <returns></returns>
        public BlendAuthenticationOptions UseSqlServerStore(string dbconn)
        {
            DBType = "SqlServer";
            DBConn = dbconn;
            return this;
        }
        #endregion

        #region token同步配置
        internal string RedisConn { set; get; }

        /// <summary>
        /// 使用redis的发布订阅实现token在多台机器间的同步
        /// </summary>
        /// <param name="dbconn">redis连接字符串</param>
        /// <returns></returns>
        public BlendAuthenticationOptions UseRedisSync(string dbconn)
        {
            RedisConn = dbconn;
            return this;
        }
        #endregion        
    }
}
